package day6;

import java.util.Scanner;

import day5.Student;

public class StudentManager2 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入学生人数：");
		int count = scanner.nextInt();
		Student[] students = new Student[count];
		// 调用方法，输入学生信息
		inputStudents(students);
		// 调用方法，输出学生信息
		outputStudents(students);
		// 调用方法，按情况查询
		searchByCondition(students);
	}

	/**
	 * 输入学生信息的方法
	 * 
	 * @param students
	 */
	public static void inputStudents(Student[] students) {
		Scanner scanner = new Scanner(System.in);
		for (int i = 0; i < students.length; i++) {
			System.out.println("请输入学生id：");
			int id = scanner.nextInt();
			System.out.println("请输入学生姓名：");
			String name = scanner.next();
			System.out.println("请输入学生班级：");
			String className = scanner.next();
			System.out.println("请输入学生年龄：");
			int age = scanner.nextInt();
			Student student = new Student(id, name, age, className);
			students[i] = student;
		}
	}

	/**
	 * 输出学生信息的方法
	 * 
	 * @param students
	 */
	public static void outputStudents(Student[] students) {
		for (int i = 0; i < students.length; i++) {
			System.out.println(students[i]);
		}
	}

	/**
	 * 按照情况进行查询的主方法
	 * 
	 * @param students
	 */
	public static void searchByCondition(Student[] students) {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			System.out.println("欢迎使用学生查询系统");
			System.out.println("根据id查询：            1");
			System.out.println("根据姓名查询：         2");
			System.out.println("根据年龄查询：         3");
			System.out.println("根据班级查询：         4");
			System.out.println("退出系统：                0");
			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			System.out.println("请输入需要使用的功能代号");
			int num = scanner.nextInt();
			if (num == 0) {
				System.out.println("退出系统");
				return;
			}
			switch (num) {
			case 1:// 根据关键词查找
				System.out.println("请输入需查询的id：");
				int id = scanner.nextInt();
				// 调用根据id查找的方法
				searchById(students, id);
				break;
			case 2:// 根据姓名查找
				System.out.println("请输入查询的姓名：");
				String name = scanner.next();
				// 调用根据姓名查找的方法
				searchByName(students, name);
				break;
			case 3:// 根据年龄查找
				System.out.println("请输入查询的年龄：");
				int age = scanner.nextInt();
				// 调用根据年龄查找的方法
				searchByAge(students, age);
				break;
			case 4:// 根据班级查找
				System.out.println("请输入查询的班级：");
				String className = scanner.next();
				// 调用根据班级查找的方法
				searchByClassName(students, className);
				break;
			default:
				break;
			}
		}
	}

	/**
	 * 按照班级名字进行查找的方法
	 * 
	 * @param students
	 * @param className
	 */
	public static void searchByClassName(Student[] students, String className) {
		boolean isSearchByClassNameFound = false;
		for (int i = 0; i < students.length; i++) {
			if (className.equals(students[i].getClassName())) {
				System.out.println(students[i]);
				isSearchByClassNameFound = true;
			}
		}
		if (isSearchByClassNameFound == false) {
			System.out.println("查找不到相关信息");
		}
	}

	/**
	 * 按照年龄进行查找的方法
	 * 
	 * @param students
	 * @param age
	 */
	public static void searchByAge(Student[] students, int age) {
		boolean isSearchByAgeFound = false;
		for (int i = 0; i < students.length; i++) {
			if (age == students[i].getAge()) {
				System.out.println(students[i]);
				isSearchByAgeFound = true;
			}
		}
		if (isSearchByAgeFound == false) {
			System.out.println("查找不到对应的信息");
		}
	}

	/**
	 * 按照姓名进行查找的方法
	 * 
	 * @param students
	 * @param name
	 */
	public static void searchByName(Student[] students, String name) {
		boolean isSearchByNameFound = false;
		for (int i = 0; i < students.length; i++) {
			if (name.equals(students[i].getName())) {
				System.out.println(students[i]);
				isSearchByNameFound = true;
			}
		}
		if (isSearchByNameFound == false) {
			System.out.println("查找不到对应的信息");
		}
	}

	/**
	 * 按照id进行查找的方法
	 * 
	 * @param students
	 * @param id
	 */
	public static void searchById(Student[] students, int id) {
		boolean isSearchByIdFound = false;// false代表没找到
		for (int i = 0; i < students.length; i++) {
			if (id == students[i].getId()) {
				System.out.println(students[i]);
				isSearchByIdFound = true;
			}
		}
		if (isSearchByIdFound == false) {
			System.out.println("查找不到对应的信息");
		}
	}
}
